Assignment: a2_Panel layouts By: Daniel B. Carr Topics: Panel layouts for small multiples in a window or on a page 1. panelLayout object for bookkeeping 2. panelLayout() arguments 3. A simple example 4. Panel functions 5. An example that labels panels 6. Modifying Exercise 5. Install: panelLayout functions Get the panelLayout.r file from the class schedule Put it in you a directory you might keep for class functions With the R Console window active use the R source item under the File Menu to locate the director and select the file. The file includes several function defintions such as panelLayout, panelSelect, panelScale, ... Due: 5. and 6. Note that for 6. you are to include your modifed script with changes in red. Note: There is a panel matrix layout function in R called layout(). This provides an alternative approach that I think is less convenient for many of mulitiple panel graphics. However it may be more convenient for some graphics. 1. Panel layout object for bookkeeping____________________________________________ The panelLayout() function creates a list object for bookkeeping The information primarily includes the positions of margin panels and a matrix of data panels for a single window or a page. Referencing a panel: The margin panels are labeled by "left","right","top", and "bottom". The matrix of panels are referenced by row and column subscript. Row 1 refers to the top row. Column 1 refers to the left column. The relative sizes for data panels: The layout specifiction provides relative size control for the row heights and column width for the panel matrix. Space is allocated after subtracting the the fixed allocations (indicated below) from the window or page size. Fixed sizes for margin Panels: The left and right margin panels have widths that are specified in inches. The top and bottom margin panels have heights that are specified in inches. The top panel default height is 1 inches. The other defaults are 0 inches. Fixed sized for panel row and column separation: The top panel, matrix row panels and the bottom panel have row separations specified in inches. The left panel, the matrix column panels and the right panel have column separations specified in inches. All default values are 0 inches. Note there there is one more on separation the matrix row and one more column separation the matrix columns Fixed sizes for the borders The values are inches in left, right,top,bottom,order. Default values are .5. The window or page size: Most R gaphics devices support specification of weight and height in inches. For example window(width=10,height=8). There are some challenges in producing very precise graphics for a window device. When I want precision I use other devices such as pdf(). Window will be good enough most of the time here are some of the potential problems. 1) Setting the window height to 20 when the screen is on only 10 inches tall. 2) R Documentation say the width argument is nominal. You may get what you specific. 3) You may not remember to take in to account the menus items space used at the top of agraphics windows. 4) The biggest problem is related to resizing windows. My panel layout functions assess the window size when panelLayout is called. The calculated panels sizes remains the same if the window is resize. Graphics drawn based on calculations before resizing will be wrong. If the window is resized before the execution panelLayout() the panel sizes may be okay but the fonts my be wrong pre-sized window. The script that involves the font size doesn't know about the window resizing. panelLayout() bookkeeping for interactive operations: Currently the locator() and identify() functions only relative to the with the last panel that has been selected and scaled. Deprecated panelLayout bookkeeping information A recurrent task is to put a label at edge of rows or columns of panel. Using mtext() when an edge panel is active serves the task. Historically there was a separate panel layout function for this task that used the locations in inches of the left and right of data panels and the top and bottom of data panels relative to the left and bottom borders. This information is still available in the panelLayout object. There are top, bottom, left, and right margin panels. There is a matrix of panels internal to the margin panels. There is a layout()function in R that provide an alternative. 2. panelLayout()arguments_______________________________________________________ panelLayout(nrow = 1, ncol = 1, leftMargin = 0, rightMargin = 0, # size in inches topMargin = 1, bottomMargin = 0, # size in inches borders = rep(0.5, 4), #borders are left, right, top, bottom rowSize=1, colSize = 1, # relative size, vector lengths are nrow and ncol rowSep =0, colSep = 0, # size in inches, vector lengths are nrow+1 and ncol+1 pad = NULL) Example: pan = panelLayout(nr=3,nc=4) Comments: 1) Shortened arguments work when there is a unique match. The "nr" in the example matched "nrow" in the function definition. 2) rowSize and colSize arguments can be vector of length nrow and ncol respectively Shorter vectors are cycled and truncated to obtain the needed length. 3) rowSep and colSep can be vectors of length nrow+1 and ncol+1 respectively. The first row separation is between the top panel and the first row of data panels. The last row separation is between the last row of data panels and the bottom panel. Hence there are nrow+1 row separators. The pattern for columns is similar. Shorter vectors are cycled and truncated to obtain the needed length. 3. A simple example______________________________________________________________ ##Run windows() myFirstLayoutObject = panelLayout(nr=3,ncol=4) for (i in 1:3){ for (j in 1:4){ panelSelect(myFirstLayoutObject,i,j) panelScale() # default arguments are c(0,1) for both x and y axes. panelOutline() }} ## 4. Panel Functions and arguments____________________________________________________ panelSelect(layout,row=1, col=1, margin=NULL) margin arguments are "left","right","top", and "bottom' The margin argument if specified panelScale(rx = c(0, 1), ry = c(0, 1), inches = FALSE) The rx and ry arguments provide the x and y axis data scales. rx[1] and rx[2] become associated with the left and right panel edges ry[1] and ry[2] become associated with the bottom and top panel edges If inches is true, the bounds start at 0 go to the number of inches. panelFill(col="#E0E0E",border=F,...) arguments are passed to polygon() panelOutline(col = "black", lwd = 1, lty = 1,...) 5. An example that labels panels____________________________________________________ ##Run windows(width=8,height=10) # Graphic parameters cex = 1.2 # character expansion size testLayout <- panelLayout(nrow=3,ncol=2, rowSep=.1, # values are replicate colSep=.25, # values are replicate leftMar=.5,right=.5,bottom=.5) panelSelect(testLayout,margin='top') panelScale() # default rx=c(0,1), ry=c(0,1) panelFill("lightBlue") text(.5,.5,'Top Margin',adj=.5,cex=2) panelSelect(testLayout,margin='bottom') panelScale() panelFill("lightBlue") text(.5,.5,'Bottom Margin',adj=.5,cex=cex) panelSelect(testLayout,margin='left') panelScale() panelFill("lightBlue") text(.5,.5,'Left Margin',adj=.5,srt=90,cex=cex) panelSelect(testLayout,margin='right') panelScale() panelFill("lightBlue") text(.5,.5,'Right Margin',adj=.5,srt=90,cex=cex) panelOutline() # Now outlines the other margin panels panelSelect(testLayout,margin='top') panelScale() panelOutline() panelSelect(testLayout,margin='bottom') panelScale() panelOutline() panelSelect(testLayout,margin='left') panelScale() panelOutline() # Now fill label and outline the data panels mycolors=c('#FF0000','#0088FF','#FFFF00') for(i in 1:3){ for (j in 1:2){ panelSelect(testLayout,i,j) panelScale() panelFill(col=mycolors[i]) panelOutline() text(.5,.5,paste("Row",i,' Col',j,sep=' '),adj=.5,cex=cex, font=ifelse(j==1,1,2), col=ifelse(i==1,"white","black")) }} ## End Using the file menu on the graphics window select copy to clipboard as a metafile. Then paste the plot into windows Notes: Once a panel in selected and scaled many things can be done, at background fill, add grid lines, add points, lines, polygon, symbols, and text. The margin panel plotting sequence above shows access to the margin panels. Often just the the top margin panels is used for titles and column labels. Note that the margins are filled before drawing outlines. The reason for that the panels overlap. The fill would cover some of the previous outline in the outlines were not last. Text is easier to read when it contrasts substantially with the background. Few combinations rival black on white and white on black for ease of reading. (I read once that visual acuity is little better with white a black background) When putting text on a white background, my alternatives to black are usually blue or red and I use red to draw attention. With black and white being 0 and 1 on a lightness (or gray level) scale, full intensity red, green and blue phosphors will be roughly .3, .6 and .1 respectively in terms of lightness. Think of white as having lightness of 1. Full intensity green at .6 lightness would have poor contrast on white. Red at .3 is better. Blue at .1 is really good. Look at signs when you drive. You will see white on red, black on yellow, black on white. For the most part there is high contrast. 6. Modifying Exercise 5 Set the interior data panel separations to zero. The rowSep vector will be of form c(?,?,?,?) and the colSep vector will be of form c(?,?,?) Change the rowSize argument so the first data row has twice the height as the other two rows. Put both the plot and the modified script with change in red in the work document with the plot from 5. Provide a link to this file on your web site.